﻿<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #container {
            border: 1px solid blue;
            width: 200px;
            height: 200px;
            background-color: red;
            opacity: 0.5;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var container = document.getElementById('container');
            container.onmouseover = function () {
                startMove(this, 400, 'height', function () {
                    startMove(container, 400, 'width');
                });
            }

            container.onmouseout = function () {
                startMove(this, 200, 'width', function () {
                    startMove(container, 200, 'height');
                });
            }
        };
        var timer = null;
        function startMove(obj, target, attr, fn) {
            clearInterval(timer);
            timer = setInterval(function () {
                var speed = (target - getStyle(obj, attr)) / 20;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                if (getStyle(obj, attr) == target) {
                    clearInterval(timer);
                    if (fn) {
                        fn();
                    }
                }
                else {
                    obj.style[attr] = getStyle(obj, attr) + speed + 'px';
                }
            }, 10)
        }
        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return parseFloat(obj.currentStyle[attr]);
            }
            else {
                return parseFloat(getComputedStyle(obj, false)[attr]);
            }
        }


    </script>
    <meta charset="utf-8" />
</head>
<body>
    <div id="container"></div>
</body>
</html>
